<?php

namespace App\Admin\Controllers;
use App\Admin\Renderable\CouponsConfigTable;
use App\Admin\Renderable\UserTable;
use App\Models\CouponsConfig as CouponsConfigModel;
use App\Models\User;
use Dcat\Admin\Form;
use App\Admin\Repositories\Coupon;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\CouponsConfig;
use Dcat\Admin\Widgets\Card;
use App\Admin\Forms\CouponAdd;
use Dcat\Admin\Layout\Content;

class CouponController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Coupon(["user"]), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('descript')->limit(10,"...");
            $grid->column('type')->display(function ($type){
                return CouponsConfig::$couponsTypeMap[$type];
            });
            $grid->column('is_used')->switch();
            $grid->column('used_date');
            $grid->column('checker');
            $grid->column('user.nickname',"所属用户");
            $grid->column('created_at')->sortable();
            $grid->column('end_date');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like("title");
                $filter->equal("type","券的性质")->select(
                    CouponsConfigModel::$couponsTypeMap
                );
                $filter->equal("coupons_config_id")->selectTable(CouponsConfigTable::make())
                    ->title("礼品券")
                    ->model(CouponsConfig::class,'id',"title");

                $filter->equal("user_id")->selectTable(UserTable::make())
                    ->title("用户选择")
                    ->model(User::class,'id',"nickname");
                });
            //禁用创建按钮
            $grid->disableCreateButton();
            $grid->disableEditButton();
            // 禁用批量删除
            $grid->disableBatchDelete();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Coupon(['user']), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('descript');
            $show->field('type')->as(function ($type){
                return CouponsConfig::$couponsTypeMap[$type];
            });
            $show->field('is_used')->using([1=>"是",0=>"否"]);
            if($show->model()->is_used==1){
                $show->field('used_date');
            }
            $show->field("checker");
            $show->field('user.nickname',"所属人物");
            $show->field('end_date');
            $show->field('created_at');
            $show->field('updated_at');
            $show->panel()
                ->tools(function($tools){
                    $tools->disableEdit();
                });
        });
    }

    protected function form()
    {
        return Form::make(new Coupon(), function (Form $form) {
            $form->display('id');
            $form->switch("is_used");
        });
    }


    public function create_by_user($coupons_config_id,Content $content)
    {
        $data['coupons_config_id'] = $coupons_config_id;
        return $content->body(new Card(new CouponAdd($data)));
    }

//
}
